<?php
require "./vendor/autoload.php";

use PhpAmqpLib\Connection\AMQPStreamConnection;
use \PhpAmqpLib\Exchange\AMQPExchangeType;

//声明连接参数
$conf = [
    'host' => PHP_OS == "Linux" ? '127.0.0.1' : '10.0.0.200',
    'vHost' => '/', //类比数据库名称
    'port' => 5672,
    'user' => 'wyqgg',
    'pwd' => '123456',
];
//连接broker

$exchangeName = 'kd_sms_send_ex'; //交换机名

$queueName = 'kd_sms_send_q'; //队列名称

$routingKey = 'sms_send'; //路由关键字(也可以省略)

//连接rabbitMq
$conn = new AMQPStreamConnection($conf['host'], $conf['port'], $conf['user'], $conf['pwd']);
if (!$conn){
    echo "连接失败";
    exit();
}
//创建连接管道
$ch = $conn->channel();
//创建交换机
$ch->exchange_declare($exchangeName, AMQPExchangeType::DIRECT, false, true, false);
//创建消息队列
$ch->queue_declare($queueName, false, true, false, false);
//绑定消息队列和交换机
$ch->queue_bind($queueName,$exchangeName , $routingKey);

function callback(\PhpAmqpLib\Message\AMQPMessage $message)
{
   echo " 消费者1接收到 " . $message->getBody() .  PHP_EOL;
   usleep(1000);//休眠
}

$ch->basic_consume($queueName, '', false, true, false, false, "callback");

while (count($ch->callbacks)) {
    $ch->wait();
}
//while ($ch->is_consuming()) {
//    $ch->wait();
//}
$ch->close();
$conn->close();